<?php
include './header.php';
$ele = "COUNT('admin_id')";
$count = $db->selectOne('admin', $ele)[0];

$admin_cond = "LIMIT {$offset},{$limit}";
$page_info = [
    'limit' => $limit,
    'size' => $size,
    'current' => $current,
    'count' => $count,
    'class' => 'pagination'
];
$pageobj = Page::getInstance($page_info);
$page = $pageobj->showpage();

$admin_all = $db->selectAll('admin', '*', $admin_cond);

//删除单个
if(isset($_GET['del_user_id']) && $_GET['del_user_id'] > 0){
    $id = $_GET['del_user_id'];  //获取要删除的id
    $del_cond = "WHERE admin_id = {$id}";      //删除条件
    $now_name = $_SESSION['username'];  //当前用户名

    if($now_name != 'admin'){
        $res = $db->delData('admin', $del_cond);
        if($res){
            echo "<script>alert('删除成功！');window.location.href='user_list.php';</script>";
            $login->logout();
        }else{
            echo "<script>alert('删除失败！');window.location.href='user_list.php';</script>";
        }
    }
}

//批量删除
if(isset($_POST['del_all'])){
    $user_ids = $_POST['idarr'];
    for($i=0; $i<count($user_ids); $i++){
        $del_conds = "WHERE admin_id = {$user_ids[$i]}";
        $res = $db->delData('admin', $del_conds);
        if($res){
            echo "<script>alert('删除成功！');</script>";
            $login->logout();
        }else{
            echo "<script>alert('删除失败！');window.reload();</script>";
        }
    }


}


?>
  <!-- Start: Content -->
  <section id="content">
    <div id="topbar" class="affix">
      <ol class="breadcrumb">
        <li><a href="index.php"><span class="glyphicon glyphicon-home"></span></a></li>
        <li class="active">系统管理员</li>
      </ol>
    </div>
    <div class="container">

	 <div class="row">
        <div class="col-md-12">
			<div class="panel">
                <div class="panel-heading">
                  <div class="panel-title">系统管理员</div>
                  <a href="user_add.php" class="btn btn-info btn-gradient pull-right"><span class="glyphicons glyphicon-plus"></span> 添加管理员</a>
                </div>
                <form action="" method="post">
                <div class="panel-body">
                  <table class="table table-striped table-bordered table-hover dataTable">
                      <tr class="active">
                        <th class="text-center" width="100"><input type="checkbox" value="" id="checkall" class=""> 全选</th>
                        <th>账号</th>
                        <th>添加时间</th>
                        <th>最后登陆时间</th>
                        <th>最后登陆所在ip</th>
                        <th width="200">操作</th>
                      </tr>
                      <?php foreach($admin_all as $val){ ?>
                    	<tr class="success">
                        <td class="text-center"><input type="checkbox" value="<?php echo $val['admin_id']; ?>" name="idarr[]" class="cbox"></td>
                        <td><?php echo $val['admin_name']; ?></td>
                        <td><?php echo date('Y-m-d h:i:s', $val['admin_reg_time']); ?></td>
                        <td><?php echo date('Y-m-d h:i:s', $val['admin_last_login']); ?></td>
                        <td><?php echo $val['admin_reg_ip']; ?></td>
                        <td>
		                      <div class="btn-group">
		                        <a href="user_edit.php?user_id=<?php echo $val['admin_id']; ?>" class="btn btn-default btn-gradient"><span class="glyphicons glyphicon-pencil"></span></a>
		                        <a onclick="return confirm('确定要删除吗？');" href="?del_user_id=<?php echo $val['admin_id']; ?>" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicons glyphicon-trash"></span></a>
		                      </div>
                        </td>
                      </tr>
                      <?php } ?>
                  </table>
                  
                  <div class="pull-left">
                  	<button type="submit" name="del_all" class="btn btn-default btn-gradient pull-right delall"><span class="glyphicons glyphicon-trash"></span></button>
                  </div>
                  
                  <?php echo $page; ?>
                  
                </div>
                </form>
              </div>
          </div>
        </div>
    </div>
  </section>
  <!-- End: Content --> 
</div>
<!-- End: Main --> 
</body>
</html>